package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gcgl.domain.YyFk;
import com.ruoyi.gcgl.mapper.YyFkMapper;
import com.ruoyi.gcgl.service.IYyFkService;

/**
 * 工程项目支付Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-06
 */
@Service
public class YyFkServiceImpl implements IYyFkService 
{
    @Autowired
    private YyFkMapper gcFkMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询工程项目支付
     * 
     * @param id 工程项目支付主键
     * @return 工程项目支付
     */
    @Override
    public YyFk selectGcFkById(Long id)
    {
        return gcFkMapper.selectGcFkById(id);
    }

    /**
     * 查询工程项目支付列表
     * 
     * @param gcFk 工程项目支付
     * @return 工程项目支付
     */
    @Override
    public List<YyFk> selectGcFkList(YyFk gcFk)
    {
        return gcFkMapper.selectGcFkList(gcFk);
    }

    /**
     * 新增工程项目支付
     * 
     * @param gcFk 工程项目支付
     * @return 结果
     */
    @Override
    public int insertGcFk(YyFk gcFk)
    {
    	int rows = gcFkMapper.insertGcFk(gcFk);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(gcFk.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", gcFk.getOwner());
        variables.put("level1", gcFk.getBmspr());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("yyfukuangshengqing", String.valueOf(gcFk.getId()), variables);
    	
        YyFk obj = new YyFk();
    	obj.setId(gcFk.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	gcFkMapper.updateGcFk(obj);
    	
        return rows;
    }

    /**
     * 修改工程项目支付
     * 
     * @param gcFk 工程项目支付
     * @return 结果
     */
    @Override
    public int updateGcFk(YyFk gcFk)
    {
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(gcFk.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", gcFk.getOwner());
//        variables.put("level1", gcFk.getApprovename());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjfukuangshengqing", String.valueOf(gcFk.getId()), variables);
//        gcFk.setProcessInstanceId(pi.getProcessInstanceId());
    	
        return gcFkMapper.updateGcFk(gcFk);
    }

    /**
     * 批量删除工程项目支付
     * 
     * @param ids 需要删除的工程项目支付主键
     * @return 结果
     */
    @Override
    public int deleteGcFkByIds(String ids)
    {
        return gcFkMapper.deleteGcFkByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工程项目支付信息
     * 
     * @param id 工程项目支付主键
     * @return 结果
     */
    @Override
    public int deleteGcFkById(Long id)
    {
        return gcFkMapper.deleteGcFkById(id);
    }

	@Override
	public int updateGcFk4check(YyFk gcFk) {
		return gcFkMapper.updateGcFk(gcFk);
	}
}
